package com.atguigu.Flink.datastream.transform;

import com.alibaba.fastjson.JSON;
import com.atguigu.Flink.POJO.Event;
import com.atguigu.Flink.function.ClickSource;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

public class Flink01_SimpleTransform {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //使用自定义数据源
        DataStreamSource<Event> ds = env.addSource(new ClickSource());
//        ds.map(new MapFunction<Event, String>() {
//            @Override
//            public String map(Event event) throws Exception {
//                return JSON.toJSONString(event);
//            }
//        }).print();
//        ds.filter(new FilterFunction<Event>() {
//            @Override
//            public boolean filter(Event event) throws Exception {
//                return event.getUser().equals("otto") || event.getUser().equals("xuanDog");
//            }
//        }).print();
        ds.flatMap(new FlatMapFunction<Event, String>() {
            @Override
            public void flatMap(Event event, Collector<String> collector) throws Exception {
                if(event.getUser().equals("otto")||event.getUser().equals("shinyRuo")){
                    collector.collect(JSON.toJSONString(event));
                }
            }
        }).print();


        try {
            env.execute();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
